﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using NAnt.SVN.Tasks;
using NAnt.Core;

namespace NAnt.SVN.Util
{
    /// <summary>
    /// Class with some url utils to convert SVN urls.
    /// $Id$
    /// </summary>
    public class UrlUtil
    {
        #region Check functions

        /// <summary>
        /// Check if a repo uri ends with trunk
        /// </summary>
        /// <param name="repo"></param>
        /// <returns></returns>
        public static bool IsTrunk(Uri repo)
        {
            return repo.AbsoluteUri.EndsWith(SVNMainRepos.TRUNK);
        }

        /// <summary>
        /// Check if a repo uri ends with tags
        /// </summary>
        /// <param name="repo"></param>
        /// <returns></returns>
        public static bool IsTags(Uri repo)
        {
            return repo.AbsoluteUri.EndsWith(SVNMainRepos.TAGS);
        }

        /// <summary>
        /// Check if a repo uri ends with branches
        /// </summary>
        /// <param name="repo"></param>
        /// <returns></returns>
        public static bool IsBranches(Uri repo)
        {
            return repo.AbsoluteUri.EndsWith(SVNMainRepos.BRANCHES);
        }

        /// <summary>
        /// Check if it contains
        /// </summary>
        /// <param name="repo"></param>
        /// <returns></returns>
        public static bool ContainsTrunk(Uri repo)
        {
            return urlsegmentContains(SVNMainRepos.TRUNK, repo);
        }

        #endregion

        #region Manipulation functions

        public static bool ConvertTrunkToTags(Uri input, out Uri output)
        {
            if (IsTrunk(input))
            {
                output = new Uri(input.AbsoluteUri.Replace(SVNMainRepos.TRUNK, SVNMainRepos.TAGS));
                return true;
            }
            else
            {
                output = null;
                return false;
            }
        }

        public static void TrimToTrunk(ref Uri input)
        {
            if (ContainsTrunk(input))
            {
                string uri = input.AbsoluteUri;
                while (!IsTrunk(new Uri(uri)))
                {
                    uri = uri.Substring(0, uri.Length - 1);
                }
                input = new Uri(uri);
            }
        }

        /// <summary>
        /// Convert a string to a uri
        /// </summary>
        /// <param name="uriString"></param>
        /// <returns></returns>
        public static Uri ConvertToUri(string uriString)
        {
            try
            {
                return new Uri(uriString);
            }
            catch(Exception e){
                throw new BuildException(string.Format(Strings.GetString("UTIL0309"), uriString), e);
            }
        }

        #endregion   

        #region Support functions

        /// <summary>
        /// Small function to test the segments of an uri
        /// </summary>
        /// <param name="segment"></param>
        /// <param name="uri"></param>
        /// <returns></returns>
        private static bool urlsegmentContains(string segment, Uri uri)
        {
            foreach (string s in uri.Segments)
            { 
                if(s.Equals(segment)) return true;
            }
            return false;
        }

        #endregion
    }
}
